package springIOC.IOC_XML.dao.impl;

import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;
import springIOC.IOC_XML.beans.User;
import springIOC.IOC_XML.dao.UserDao;
import springIOC.IOC_XML.handler.BeanResultSetHandler;
import springIOC.IOC_XML.handler.ListBeanResultSetHandler;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

@Repository("userDao")
@Scope("prototype")
public class UserDaoImpl implements UserDao {

    @Resource(name = "queryRunner")
    private QueryRunner runner;

//    public QueryRunner getRunner() {
//        return runner;
//    }

    public void setRunner(QueryRunner runner) {
        this.runner = runner;
    }

    @Override
    public User findUserByID(int uid) {
        String sql="select * from tab_user where uid = ?";
        User user=null;
        try {
            user=runner.query(sql,new BeanResultSetHandler<User>(User.class),uid);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return user;

    }

    public List<User> findUserByName(String name){
        String sql="select * from tab_user where name = ?";
        List<User> users=null;
        try {
            users=runner.query(sql,new ListBeanResultSetHandler<User>(User.class),name);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return users;
    }
}
